﻿//https://leetcode.cn/problems/find-median-from-data-stream/?envType=study-plan-v2&envId=top-100-liked

class MedianFinder {
    priority_queue<int> left;
    priority_queue<int, vector<int>, greater<int>> right;
public:
    MedianFinder() {

    }

    void addNum(int num)
    {
        if (left.size() == right.size())
        {
            if (left.size() == 0 || num <= left.top())
            {
                left.push(num);
            }
            else
            {
                right.push(num);
                left.push(right.top());
                right.pop();
            }
        }
        else
        {
            if (num <= left.top())
            {
                left.push(num);
                right.push(left.top());
                left.pop();
            }
            else
            {
                right.push(num);
            }
        }
    }

    double findMedian() {
        if (left.size() == right.size())
            return (left.top() + right.top()) / 2.0;

        return left.top();
    }
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

//https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/submissions/617277855/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int maxProfit(vector<int>& prices)
    {
        int ret = 0;
        int minprice = prices[0];
        for (auto& e : prices)
        {
            ret = max(ret, e - minprice);
            minprice = min(minprice, e);
        }
        return ret;
    }
};

//https://leetcode.cn/problems/jump-game/submissions/617279314/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    bool canJump(vector<int>& nums)
    {
        int n = nums.size();
        int left = 0, right = 0, maxpos = 0;

        while (left <= right)
        {
            if (maxpos >= n - 1)
            {
                return true;
            }

            for (int i = left; i <= right; i++)
            {
                maxpos = max(maxpos, nums[i] + i);
            }
            left = right + 1;
            right = maxpos;
        }

        return false;
    }
};